import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'
const TokenExpiresKey = 'Token-Expires'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function getTokenExpires() {
  const expries = Cookies.get(TokenExpiresKey)
  return new Date(expries)
}

export function setTokenExpires(tokenExpires) {
  let time = tokenExpires
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof (time) === 'string') {
      time = new Date(time.replace(/-/g, '/'))
    }
    if (typeof (time) === 'number' && ('' + time).length === 10) {
      time = parseInt(time) * 1000
    }
    date = new Date(time)
  }
  const expries = date.getTime()
  return Cookies.set(TokenExpiresKey, expries)
}

export function removeTokenExpires() {
  return Cookies.remove(TokenExpiresKey)
}

